package com.cms.recursoshumanos.ui.beneficios;

import com.cms.recursoshumanos.cache.ListaBeneficiosAdmCache;
import com.cms.recursoshumanos.cache.UserInfoCache;
import com.cms.recursoshumanos.component.BeneficioAdmListButtonField;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.container.ForegroundManager;
import com.cms.recursoshumanos.container.ListStyleButtonSetScrollManager;
import com.cms.recursoshumanos.container.SearchBarManager;
import com.cms.recursoshumanos.engine.ScreenEngine;
import com.cms.recursoshumanos.handler.BeneficiosAdministrativosScreenHandler;
import com.cms.recursoshumanos.object.BeneficioAdministrativo;
import com.cms.recursoshumanos.object.list.ListaBeneficioAdministrativo;
import com.cms.recursoshumanos.popup.ShowDialogAlert;
import com.cms.recursoshumanos.ui.RecursosHumanosScreen;

import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.VerticalFieldManager;



/**
 * @author Francisco Trompiz
 *
 * Corporate Mobile Solutions. 2013
 */

public class ListaBeneficioAdministrativoScreen extends RecursosHumanosScreen implements FieldChangeListener 
{

	
	SearchBarManager sbm;
	ListStyleButtonSetScrollManager buttonSet;
	VerticalFieldManager vfm;
	LabelField lblBeneficioAdm;
	/*TwoColumnFieldManager twoColumnManager;*/

	private static ListaBeneficioAdministrativoScreen instance = null;
	
	private ListaBeneficioAdministrativo lista_beneficios;
	
	public static ListaBeneficioAdministrativoScreen getInstance(ListaBeneficioAdministrativo listado)
	{
		if(instance == null)
    	{
    		instance = new ListaBeneficioAdministrativoScreen(listado);
    	}
		else
		{
			instance.lista_beneficios = listado;
			instance.reset();
		}
    	return instance;
    }
	
	
    private ForegroundManager _foreground;    
    
	public ListaBeneficioAdministrativoScreen(ListaBeneficioAdministrativo listado) 
	{
		super( NO_VERTICAL_SCROLL | USE_ALL_HEIGHT );
		
		lista_beneficios = listado;	
		
		setComponents();
	}
			
	
	protected void makeMenu(Menu menu, int instance) 
	{
		MenuItem refreshButton = new MenuItem("Actualizar", 0, 0) 
        {
			public void run() 
			{
				BeneficiosAdministrativosScreenHandler handler = new BeneficiosAdministrativosScreenHandler();
	   			handler.refreshBeneficiosAdministrativos(UserInfoCache.getInstance().getUsuario().getNombre_Usuario(),
	   					UserInfoCache.getInstance().getUsuario().getPassword());
			}
		};
		menu.add(refreshButton);
		super.makeMenu(menu, instance);
	}


	public void reset() 
	{

		try 
		{
			if(buttonSet.getFieldCount() > 0) 
			{
				//sbm.setFocus();
				buttonSet.deleteAll();
			}
		} 
		catch (Exception e) 
		{
			ShowDialogAlert alert = new ShowDialogAlert("Exception in delete fields...");
			alert.run();
		}
		showListField();
		//pis = Json.getInstance().getPIs();
	}

	public void setComponents() 
	{
		setTitle("Beneficios de LOTTT");
		vfm = new VerticalFieldManager( Field.FIELD_HCENTER | VerticalFieldManager.NON_FOCUSABLE );
		_foreground = new ForegroundManager(NO_VERTICAL_SCROLL);
		
        buttonSet = new ListStyleButtonSetScrollManager();
        buttonSet.setScreen(this);
        lblBeneficioAdm = new LabelField("Escoja una Opcion", FIELD_HCENTER | DrawStyle.HCENTER);
        
        Field separator = new SeparatorField();
        separator.setMargin( 2, 0, 2, 0 );
        
		sbm = new SearchBarManager(SearchBarManager.STRING_FILTER)
		{
			protected boolean keyChar(char ch, int status, int time) 
			{
				switch (ch) 
			    {
			        case Characters.BACKSPACE:
			        	if (sbm.getSearchText().length() != 0)
			        	{
			        		String texto = sbm.getSearchText().substring(0, sbm.getSearchText().length() - 1);
			        		if (texto.length() == 0)
			        		{
			        			setListadoCompleto();
			        		}
			        		else
			        		{
			        			setListadoParam(texto);
			        		}
			        	}
			        	return super.keyChar(ch, status, time);
			        case Characters.ENTER:

			            
			            return true;
			        
			        default:
			        	
			        	setListadoParam(sbm.getSearchText()+ch);
			        	
			        	return super.keyChar(ch, status, time);

			    }
				
			}
		};
		showListField();
		
        

		_foreground.add(sbm);
		_foreground.add(lblBeneficioAdm);
		_foreground.add( separator );
		_foreground.add(buttonSet);
		
        add( _foreground );
	}
	public void setListadoCompleto()
	{
		lista_beneficios = ListaBeneficiosAdmCache.getInstance().getListaBeneficio();
		this.reset();
		//twoColumnManager.invalidate();
		_foreground.invalidate();
	}
	public void setListadoParam(String buscador)
	{
		int textlength = buscador.length();
		ListaBeneficioAdministrativo aux_listado_beneficio = ListaBeneficiosAdmCache.getInstance().getListaBeneficio();
		ListaBeneficioAdministrativo listado_benefico_mostrar = new ListaBeneficioAdministrativo();
		for (int i = 0; i < aux_listado_beneficio.cantElementListaBeneficioAdminsitrativo(); i++) 
		{
			BeneficioAdministrativo beneficio_aux = aux_listado_beneficio.getBeneficioAtIndex(i);
	   
			String nombre_beneficio = ""+beneficio_aux.getNombreBeneficioAdministrativo();
	   
			if (textlength <= nombre_beneficio.length()) 
			{
				if (buscador.equalsIgnoreCase((String) nombre_beneficio.substring(0,textlength))) 
				{
					listado_benefico_mostrar.addBeneficioAdministrativo(beneficio_aux);
				}
			}
		}
		lista_beneficios = listado_benefico_mostrar;
		this.reset();
		_foreground.invalidate();
	}
	public void showListField()
	{
		
		buttonSet.deleteAll();
		BeneficioAdmListButtonField link;

		for(int i = 0; i < lista_beneficios.cantElementListaBeneficioAdminsitrativo() ; i++) 
		{

			link = new BeneficioAdmListButtonField(lista_beneficios.getBeneficioAtIndex(i), LabelField.ELLIPSIS) 
			{
			
				protected void paintBackground( Graphics g )
				{
					  COLOR_BACKGROUND_FOCUS = Constant.COLOR_LIST_FOCUS;
					  COLOR_BACKGROUND_FOCUS_ACTION = Constant.COLOR_FOCUS_NORMAL;

				      if( _drawPosition < 0 ) 
				      {
				          // it's like a list field, let the default background be drawn
				          super.paintBackground( g );
				          return;
				      }
				      
				      int oldColour = g.getColor();
				      
				      int focus = COLOR_BACKGROUND_FOCUS;
				      if(_doAction) 
				    	  focus = COLOR_BACKGROUND_FOCUS_ACTION;
				      
				      int background = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? focus : COLOR_BACKGROUND;
				      try 
				      {
				          if( _drawPosition == 0 ) 
				          {
				              // Top
				              g.setColor( background );
				              g.fillRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
				          } 
				          else if( _drawPosition == 1 ) 
				          {
				              // Bottom 
				              g.setColor( background );
				              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				          } 
				          else if( _drawPosition == 2 ) 
				          {
				              // Middle
				              g.setColor( background );
				              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
				          } 
				          else 
				          {
				              // Single
				              g.setColor( background );
				              g.fillRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
				          }
				      } 
				      finally 
				      {
				          g.setColor( oldColour );
				      }
				}
			};
			link.setChangeListener( this );
			buttonSet.add( link );
		}

	}
	public void fieldChanged( Field field, int context ) 
	{
		ScreenEngine.getInstance().goProcesioBeneficoAdminsitrativo(
				ListaBeneficiosAdmCache.getInstance().getListaBeneficio().getBeneficioAtIndex(field.getIndex()));
	}	
}


